package com.w3c.pragmatic;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.w3c.pragmatic.entity.BaseEntity;
import org.junit.platform.commons.util.StringUtils;

import java.util.Scanner;

/**
 * 代码生成工具类
 *
 * @author wangdong
 */
public class CodeGenerator {

  /**
   * main函数启动
   *
   * @param args 入参
   */
  public static void main(String[] args) {
    AutoGenerator autoGenerator = new AutoGenerator();
    // 项目本地路径（一般是本服务在各自开发者本地的路径）
    String projectPath = "C:\\wangdong\\projects\\common-production\\w3c-web-common";
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("wangdong");
    gc.setOpen(false);
    gc.setSwagger2(true);
    gc.setBaseResultMap(true);
    gc.setBaseColumnList(true);
    gc.setFileOverride(true);
    autoGenerator.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/mydb");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("123456");
    autoGenerator.setDataSource(dsc);
    // 包配置(entity/mapper/service)
    PackageConfig pc = new PackageConfig();
    pc.setParent("com.w3c.pragmatic");
    pc.setEntity("entity");
    pc.setMapper("mapper");
    //pc.setXml("classpath:/mapper/");
    pc.setService("service");
    pc.setServiceImpl("service.impl");
    autoGenerator.setPackageInfo(pc);
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(false);
    // 公共父类
    strategy.setSuperEntityClass(BaseEntity.class);
    strategy.setEntitySerialVersionUID(false);
    strategy.setSuperEntityColumns("id");
    strategy.setTablePrefix("t_");
    strategy.setSuperEntityColumns("create_time");
    strategy.setSuperEntityColumns("creator");
    strategy.setSuperEntityColumns("update_time");
    strategy.setSuperEntityColumns("updater");
    strategy.setSuperEntityColumns("remark");
    strategy.setEntityBooleanColumnRemoveIsPrefix(true);
    strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(pc.getModuleName() + "_");
    autoGenerator.setStrategy(strategy);
    autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
    autoGenerator.execute();
  }

  /**
   * 控制台输入
   *
   * @param tip 输入内容
   * @return String
   */
  public static String scanner(String tip) {
    Scanner scanner = new Scanner(System.in);
    StringBuilder stringBuilder = new StringBuilder().append("请输入").append(tip).append(":");
    System.out.println(stringBuilder);
    if (scanner.hasNext()) {
      String ipt = scanner.next();
      if (StringUtils.isNotBlank(ipt)) {
        return ipt;
      }
    }
    return null;
  }
}
